﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace DbJobsApi
{
    /// <summary>
    /// 本地日志，慎用可能不适合生产
    /// </summary>
    public class LogHelper
    {
        protected LogHelper()
        {
        }
        private readonly static ILoggerFactory _loggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(b =>
        {
            b.ClearProviders();
            b.AddSimpleConsole(b => b.SingleLine = true);
        });
        private readonly static ILogger logger  = _loggerFactory.CreateLogger<LogHelper>();

        /// <summary>
        /// 输出控制台日志
        /// </summary>
        /// <param name="log">日志内容</param>
        public static void Write(string log) => logger.LogInformation(log);
        public static void Write(string log, LogLevel logLevel)
        {
            if (logLevel == LogLevel.Error)
            {
                Console.WriteLine($"error: {log}");
            }
            else
            {
                logger.Log(logLevel, log);
            }
        }
        public static void Write(string log, Exception ex) => logger.Log(LogLevel.Trace, ex, log);

    }
}
